home *** CD-ROM | disk | FTP | other *** search
- Path: news.halcyon.com!usenet
- From: normanb@halcyon.com (Norm Bryar)
- Newsgroups: comp.lang.c++
- Subject: Re: Operator Overloading
- Date: Fri, 05 Jan 1996 17:52:56 GMT
- Organization: Northwest Nexus Inc.
- Message-ID: <4cjoh2$sn0@news.halcyon.com>
- References: <4chdhb$g64@news.cs.hope.edu> <4chht5$s2h@gold.datalytics.com>
- NNTP-Posting-Host: blv-pm3-ip9.halcyon.com
- X-Newsreader: Forte Free Agent 1.0.82
-
- Rob Stewart <stew@datalytics.com> wrote:
-
- >vnopstal@cs.hope.edu (Michael Van Opstall) wrote:
- >>A couple of questions on operator overloading:
- >>
- >>1. If you overload ==, do you have to overload != as well?
- >>
- >If you supply the one, someone will code the opposite
- >conditional sometime and expect to be able to use the other.
- >Likewise, if you provide ++(+, *, etc.), you should seriously
- >consider --(-, /, etc.).
-
- Bearing in mind, of course, the operator!=() is a freebie.
-
- BOOL myclass::operator!=( const myclass & toCompare )
- {
- return !(*this == toCompare);
- }
-
- >>2. What is the accepted method for overloading >> and <<? The way I do it
- >>is not very cool and not slick at all. Should I be including streams and doing
- >>stream manipulation? That makes sense.
- >>
- >If you're doing streams manipulation, do streams manipulation.
- >Remember, >> and << were (are) bit shift operators. The
- >iostreams classes changed that behavior. Was that the right
- >thing to do? Perhaps for your class bit shifting is the right
- >behavior for these operators.
-
- The way I've most often seen it is
- class myclass {
- friend ostream & operator<<( ostream & os, myclass & anObj );
- ...
- };
-
- ostream & operator<<( ostream & os, myclass & onObj )
- {
- os << (int) anObj.m_enum1 << (int) anObj.m_Bool1 ...
- }
-
-
- Hope this helps. --Norm
-
-